package com.hongyun.tms.web.controller;


import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.annotation.excel.ExcelExport;
import com.hongyun.tms.common.dto.mship.MShipImportDto;
import com.hongyun.tms.common.dto.mship.MShipQueryDto;
import com.hongyun.tms.common.dto.mship.MShipSaveReqDto;
import com.hongyun.tms.common.dto.mship.MShipTemplateDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.MShipVO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.entity.MShip;
import com.hongyun.tms.infra.excel.EasyExcelUtil;
import com.hongyun.tms.infra.excel.listener.MShipExcelListener;
import com.hongyun.tms.service.IMShipService;
import com.hongyun.tms.web.utils.FileUploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 大船规范 前端控制器
 * </p>
 *
 * @author liPeng
 * @since 2024-06-11
 */
@Api(tags = "大船规范管理")
@RestController
@RequestMapping("/mShip")
public class MShipController {
    @Resource
    private IMShipService shipService;

    /**
     * 分页查询
     *
     * @return page
     */
    @GetMapping("list")
    public AjaxResult<PageResponseDto<MShipVO>> list(MShipQueryDto query) {
        PageResponseDto<MShipVO> result = shipService.pageList(query);
        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询
     *
     * @return MShipVO
     */
    @GetMapping("getById")
    public AjaxResult<MShipVO> getById(@RequestParam("id") Long id) {
        MShip mShip = shipService.getById(id);
        return AjaxResult.success(MapperingUtils.copyProperties(mShip, MShipVO.class));
    }

    /**
     * 批量删除
     *
     * @return res
     */
    @PostMapping("removeBatch")
    @ApiOperation("批量删除大船规范")
    public AjaxResult removeBatch(@RequestParam(name = "ids") String idList) {
        List<String> ids = Arrays.asList(idList.split(","));
        shipService.deleteBatch(ids);
        return AjaxResult.success();
    }

    /**
     * 新增/修改
     *
     * @return MRateVO
     */
    @PostMapping("save")
    @ApiOperation("新增/修改大船规范")
    public AjaxResult save(@RequestBody @Validated MShipSaveReqDto dto) {
        shipService.saveOrUpdateShip(dto);
        return AjaxResult.success();
    }

    @ApiOperation("导入船舶")
    @PostMapping("/importShip")
    public AjaxResult<Void> importShip(MultipartFile file) throws IOException {
        AjaxResult<Void> validationResult = FileUploadUtil.validateExcelFile(file);
        // 根据code判断校验是否通过
        if (validationResult.getCode() != HttpStatus.OK.value()) {
            return validationResult;
        }

        // 创建 Excel 数据处理监听器
        MShipExcelListener listener = new MShipExcelListener(shipService);

        // 处理 Excel 文件
        try (InputStream inputStream = file.getInputStream()) {
            EasyExcelUtil.syncReadModel(inputStream, listener, MShipImportDto.class, 0, 1);
            return AjaxResult.success();
        }
    }
    @GetMapping("exportTemplate")
    @ExcelExport(value = MShipTemplateDto.class)
    public AjaxResult exportTemplate(HttpServletResponse response) {
        return AjaxResult.success(Collections.emptyList());
    }

    @GetMapping("exportData")
    @ExcelExport(value = MShipVO.class)
    public AjaxResult<List<MShipVO>> exportData(MShipQueryDto query, HttpServletResponse response) {
        List<MShipVO> exportList = shipService.export(query);
        return AjaxResult.success(exportList);
    }
}
